<?PHP
require "../../../mainfile.php";
if(!isset($_POST['action']) && !isset($_GET['action'])){
	header("Location:index.php?op=post&currentID=0");
}elseif(isset($_POST['action'])){
	$action = $_POST['action']; 
}elseif(isset($_GET['action'])){
	$action = $_GET['action']; 
}

$myts =& MyTextSanitizer::getInstance();
//REGIONAL MySQL CONNECT INFO
if($xoopsModuleConfig['enableSyndication'] == 1){
	$link = mysql_connect($xoopsModuleConfig['syndicationDB_Host'], $xoopsModuleConfig['syndicationDB_User'], $xoopsModuleConfig['syndicationDB_Passaword']) or ($error = mysql_error());
}	

if($action == 'newstory' || $action == 'update_newstory'){//INSERT NEW STORY
include_once XOOPS_ROOT_PATH .'/class/uploader.php';

		//GET OLD MEDIA INFO SO IT CAN BE DELETED IF NECESSARY		
		if(!isset($_POST['regionalOnly']) && isset($_POST['ID']) && $action == 'update_newstory'){
			$result = $xoopsDB->query( "SELECT image, imageTNwidth, soundfile, storyID, syndicate FROM ".$xoopsDB->prefix("local_news_stories")." WHERE ID = '".$_POST['ID']."'" ) ;
			if ( $result ) {
				$myrow = $xoopsDB->fetchArray($result);
			}	
		}elseif($action == 'update_newstory'){//regional DB
			mysql_select_db($xoopsModuleConfig['syndicationDB']) or ($error = "Could not select database");
			$Query = "SELECT image, imageTNwidth, soundfile, storyID, syndicate FROM CA_SE_News WHERE ID = '".$_POST['ID']."'" ;
			mysql_select_db($xoopsModuleConfig['syndicationDB']) or ($error = "Could not select database");
			$result = mysql_query ($Query, $link) or ($error = "Query failed");
			if ($result){
				$myrow = mysql_fetch_array($result, MYSQL_ASSOC);
			}
		}
		
		//upload mp3 file...
		$allowed_mimetypes = array('audio/mpeg');
		$maxfilesize = '150000000';
		$uploader = new XoopsMediaUploader(XOOPS_ROOT_PATH . "/" . "modules/local_news/media/audio/", $allowed_mimetypes, $maxfilesize);
		if ($uploader->fetchMedia($_POST["xoops_upload_file"][0])) {
		  if (!$uploader->upload()) {
			  $errors = $uploader->getErrors();
		   } else {
			  $fileName = $uploader->getSavedFileName();
			  $path = $uploader->getSavedDestination();
			  
			  //BLAST OLD FILE TO KEEP CLUTTER DOWN 
		  	  if(!isset($_POST['regionalOnly'])){//local DB
				  if(isset($myrow['soundfile']) && $myrow['soundfile'] != $fileName){
					unlink(XOOPS_ROOT_PATH."/modules/local_news/media/audio/".$myrow['soundfile']);
					}
			  }elseif(isset($_POST['regionalOnly'])){//blast regional stuff
				    if(isset($myrow['soundfile']) && (!eregi($xoopsModuleConfig['stationLetters'],$myrow['soundfile']) || !eregi($fileName, $myrow['soundfile'])) ){//Delete if Old file exists and (file is from different station or different file name) 
						if(eregi('kcaw', $myrow['soundfile'])){
						 	unlink('/home/coastalaska/kcaw/modules/local_news/media/audio/'.str_replace ("/", "", strrchr ($myrow['soundfile'], "/")));
						}elseif(eregi('kfsk', $myrow['soundfile'])){
						 	unlink('/home/coastalaska/kfsk/modules/local_news/media/audio/'.str_replace ("/", "", strrchr ($myrow['soundfile'], "/")));
						}elseif(eregi('krbd', $myrow['soundfile'])){
						 	unlink('/home/coastalaska/krbd/modules/local_news/media/audio/'.str_replace ("/", "", strrchr ($myrow['soundfile'], "/")));
						}elseif(eregi('kstk', $myrow['soundfile'])){
						 	unlink('/home/coastalaska/kstk/modules/local_news/media/audio/'.str_replace ("/", "", strrchr ($myrow['soundfile'], "/")));
						}
					}
				}
			  //END BLAST OLD FILE TO KEEP CLUTTER DOWN 
		   }
		 } else { 
		   $errors = $uploader->getErrors();
		 }
		 
		 //echo $fileName;
		
		//#############################################################################
		//upload image file..
		//#############################################################################
		
		$allowed_mimetypes = array('image/jpeg', 'image/pjpeg', 'image/x-png','image/png');
		$maxfilesize = '10000000';
		//$maxfilewidth = 145;
		//$maxfileheight = 300;
		$uploader = new XoopsMediaUploader(XOOPS_ROOT_PATH . "/" . "modules/local_news/media/pictures/", $allowed_mimetypes, $maxfilesize);
		if ($uploader->fetchMedia($_POST["xoops_upload_file"][1])) {
		  if (!$uploader->upload()) {
			  $errors2 = $uploader->getErrors();
		   } else {
			  $image = $uploader->getSavedFileName();
			  //BLAST OLD FILE TO KEEP CLUTTER DOWN 
			  if(!isset($_POST['regionalOnly']) && isset($myrow['image'])){//local DB
				  if($myrow['image'] != $image){
					unlink(XOOPS_ROOT_PATH."/modules/local_news/media/pictures/".$myrow['image']);
					unlink(XOOPS_ROOT_PATH."/modules/local_news/media/pictures/".substr_replace($myrow['image'], '-TN.jpg', -4));
					}
			 }elseif(isset($_POST['regionalOnly']) && isset($myrow['image'])){//blast regional stuff  
				    if($myrow['image'] && (!eregi($xoopsModuleConfig['stationLetters'],$myrow['image']) || !eregi($image, $myrow['image'])) ){//different file name or file is from different station
						if(eregi('kcaw', $myrow['image'])){
						 	unlink('/home/coastalaska/kcaw/modules/local_news/media/pictures/'.str_replace ("/", "", strrchr ($myrow['image'], "/")));
						 	unlink('/home/coastalaska/kcaw/modules/local_news/media/pictures/'.substr_replace(str_replace ("/", "", strrchr ($myrow['image'], "/")), '-TN.jpg', -4));
						}elseif(eregi('kfsk', $myrow['image'])){
						 	unlink('/home/coastalaska/kfsk/modules/local_news/media/pictures/'.str_replace ("/", "", strrchr ($myrow['image'], "/")));
						 	unlink('/home/coastalaska/kfsk/modules/local_news/media/pictures/'.substr_replace(str_replace ("/", "", strrchr ($myrow['image'], "/")), '-TN.jpg', -4));
						}elseif(eregi('krbd', $myrow['image'])){
						 	unlink('/home/coastalaska/krbd/modules/local_news/media/pictures/'.str_replace ("/", "", strrchr ($myrow['image'], "/")));
						 	unlink('/home/coastalaska/krbd/modules/local_news/media/pictures/'.substr_replace(str_replace ("/", "", strrchr ($myrow['image'], "/")), '-TN.jpg', -4));
						}elseif(eregi('kstk', $myrow['image'])){
						 	unlink('/home/coastalaska/kstk/modules/local_news/media/pictures/'.str_replace ("/", "", strrchr ($myrow['image'], "/")));
						 	unlink('/home/coastalaska/kstk/modules/local_news/media/pictures/'.substr_replace(str_replace ("/", "", strrchr ($myrow['image'], "/")), '-TN.jpg', -4));
						}
					}
				}
				
               //END BLAST OLD FILE TO KEEP CLUTTER DOWN
				//resize image for thumbnail if GD is installed... 
				if(function_exists("gd_info")){
					require 'thumbnail_generator.php';
					$imageTNwidth = createthumb(XOOPS_ROOT_PATH . "/" . "modules/local_news/media/pictures/".$image,XOOPS_ROOT_PATH . "/" . "modules/local_news/media/pictures/".substr($image, 0, -4)."-TN.jpg",150,150);
				}
				//end resize image for thumbnail if GD is installed...
		   }
		   
	 } else {
	   $errors2 = $uploader->getErrors();
	 }
		
		
		
		if(!isset($imageTNwidth) && isset($myrow) && $myrow['imageTNwidth'] != 0){ //use existing size from DB
			$imageTNwidth = $myrow['imageTNwidth'];
		}elseif(!isset($imageTNwidth)){//no image or no new image
			$imageTNwidth = "";
		}//otherwise there is an image and leave it alone
				
		//#############################################################################
		//END upload image file..
		//#############################################################################		
		
		$date = explode("/",$_POST['publish_date']['date']);
		$publish_date = mktime(date("H",$_POST['publish_date']['time']),date("i",$_POST['publish_date']['time']),date("s",$_POST['publish_date']['time']),$date[1],$date[2],$date[0]);
		$date = explode("/",$_POST['expiration_date']['date']);
		$expiration_date = mktime(date("H",$_POST['expiration_date']['time']),date("i",$_POST['expiration_date']['time']),date("s",$_POST['expiration_date']['time']),$date[1],$date[2],$date[0]);
		
		
			function isSoundfile($syndication='false', $updateOrNew, $regionalOnly='false'){
				global $fileName, $xoopsDB, $xoopsConfig, $xoopsModule, $myrow;
				if($regionalOnly==1 && $fileName == ''){//regional only post; no new sound file
					return "soundfile ='".$myrow['soundfile']."',";
				}elseif($regionalOnly==1 && $fileName != ''){//regional only post; new sound file
					return "soundfile ='". XOOPS_URL_NON_SSL."/modules/local_news/media/audio/".$fileName."',";
				}elseif($fileName != '' && $syndication=='false'){
					return "soundfile = '".xoops_trim($fileName)."',";
				}elseif($fileName != '' && $syndication=='true'){
					return "soundfile ='". XOOPS_URL_NON_SSL."/modules/local_news/media/audio/".$fileName."',";
				}elseif($fileName == '' && $syndication=='true' && $myrow['soundfile'] != '' && $updateOrNew != 'new'){
					return "soundfile ='". XOOPS_URL_NON_SSL."/modules/local_news/media/audio/".$myrow['soundfile']."',";
				}elseif($fileName == '' && $syndication=='true' && $myrow['soundfile'] != '' && $updateOrNew == 'new'){
					return XOOPS_URL_NON_SSL."/modules/local_news/media/audio/".$myrow['soundfile'];
				}else{
					return "";
				}

			
			}
			
			 function isImagefile($syndication='false', $updateOrNew, $regionalOnly='false'){
				global $image, $xoopsDB, $xoopsConfig, $xoopsModule, $myrow;
				if($regionalOnly==1 && $image == ''){//regional only post; no new image file
					return "image ='".$myrow['image']."',";
				}elseif($regionalOnly==1 && $image != ''){//regional only post; new image file
					return "image ='". XOOPS_URL_NON_SSL."/modules/local_news/media/pictures/".$image."',";
				}elseif($image != '' && $syndication=='false'){
					return "image = '".xoops_trim($image)."',";
				}elseif($image != '' && $syndication=='true'){
					return "image ='". XOOPS_URL_NON_SSL."/modules/local_news/media/pictures/".$image."',";
				}elseif($image == '' && $syndication=='true' && $myrow['image'] != ''  && $updateOrNew != 'new'){
					return "image ='".XOOPS_URL_NON_SSL."/modules/local_news/media/pictures/".$myrow['image']."',";
				}elseif($image == '' && $syndication=='true' && $myrow['image'] != '' && $updateOrNew == 'new'){
					return XOOPS_URL_NON_SSL."/modules/local_news/media/pictures/".$myrow['image'];
				}else{
					return "";
				}
			
			}
			


		if($action == 'update_newstory'){
		//##############################################
		//############### DEAL WITH SYNDICATION DB #####
		//##############################################
		if($_POST['syndicate'] == 1 && $myrow['syndicate'] != 1 && !isset($_POST['regionalOnly'])){//never been syndicated before - Create unique Story ID for syndication
			if(!isset($_POST['regionalOnly'])){//use station Letters to generate storyID 
			  $storyID = $xoopsModuleConfig['stationLetters'] . "-".date("dis");
			 }else{
			  $storyID = "REGI" . "-".date("dis");
			 }
			  $Query = "INSERT INTO CA_SE_News values ('0','$storyID','".htmlspecialchars (xoops_trim($_POST['headline']),ENT_QUOTES)."','".htmlspecialchars (xoops_trim($_POST['byline']),ENT_QUOTES)."','".htmlspecialchars (xoops_trim($_POST['dateline']),ENT_QUOTES)."','".htmlspecialchars (xoops_trim($_POST['tease']),ENT_QUOTES)."','".isSoundfile('true','new')."','".$_POST['syndicate']."','".xoops_trim($_POST['copyright'])."','".$publish_date."','".xoops_trim($expiration_date)."','".isImagefile('true','new')."','$imageTNwidth','".htmlspecialchars (xoops_trim($_POST['credit']),ENT_QUOTES)."','".htmlspecialchars (xoops_trim($_POST['caption']),ENT_QUOTES)."','".htmlspecialchars (xoops_trim($_POST['body']),ENT_QUOTES)."')";
			  mysql_select_db($xoopsModuleConfig['syndicationDB']) or ($error = "Could not select database");
			  $result = mysql_query ($Query, $link) or ($error = "Query failed");
			if ($result){
				$syndicateMessage = 'This story was succesfully syndicated.';
			}else {
				$syndicateMessage = 'There was a problem syndicating this story.  Check your syndication DB settings and try again:<P><B><FONT COLOR=\'red\'>'.$error.'</FONT></B>';
			}

		}elseif($_POST['syndicate'] == 0 && $myrow['syndicate'] == 1 && !isset($_POST['regionalOnly'])){//remove from syndication DB
			$storyID = "";
			$Query = "DELETE FROM CA_SE_News WHERE storyID = '".$myrow['storyID']."'";
			mysql_select_db($xoopsModuleConfig['syndicationDB']) or ($error = "Could not select database");
			$result = mysql_query ($Query, $link) or ($error = "Query failed");
			if ($result){
				$syndicateMessage = 'This story was succesfully removed from syndication.';
			}else {
				$syndicateMessage = 'There was a problem removing this story from syndication.  Check your syndication DB settings and try again:<P><B><FONT COLOR=\'red\'>'.$error.'</FONT></B>';
			}

		}elseif($_POST['syndicate'] == 1 && !isset($_POST['regionalOnly'])){//UPDATE STORY ON SYNDICATION DB
			$storyID = $myrow['storyID'];
			$Query = "UPDATE CA_SE_News SET headline = '".htmlspecialchars (xoops_trim($_POST['headline']),ENT_QUOTES)."', byline = '".htmlspecialchars (xoops_trim($_POST['byline']),ENT_QUOTES)."', dateline = '".htmlspecialchars (xoops_trim($_POST['dateline']),ENT_QUOTES)."', tease = '".htmlspecialchars (xoops_trim($_POST['tease']),ENT_QUOTES)."', ".isSoundfile('true')." syndicate = '".$_POST['syndicate']."', copyright = '".xoops_trim($_POST['copyright'])."', publish_date = '".$publish_date."', expiration_date = '".xoops_trim($expiration_date)."', ".isImagefile('true')." imageTNwidth = '$imageTNwidth', credit = '".htmlspecialchars (xoops_trim($_POST['credit']),ENT_QUOTES)."', caption = '".htmlspecialchars (xoops_trim($_POST['caption']),ENT_QUOTES)."', body = '".htmlspecialchars (xoops_trim($_POST['body']),ENT_QUOTES)."' WHERE storyID = '".$myrow['storyID']."'";
			 mysql_select_db($xoopsModuleConfig['syndicationDB']) or ($error = "Could not select database");
			 $result = mysql_query ($Query, $link) or ($error = "Query failed");
			if ($result){
				$syndicateMessage = 'This story was succesfully updated in the syndication database.';
			}else {
				$syndicateMessage = 'There was a problem updating this story in the syndication Database.  Check your syndication DB settings and try again:<P><B><FONT COLOR=\'red\'>'.$error.'</FONT></B>';
			}
		}elseif($_POST['syndicate'] == 1 && isset($_POST['regionalOnly'])){//UPDATE STORY ONLY ON SYNDICATION DB
			$Query = "UPDATE CA_SE_News SET headline = '".htmlspecialchars (xoops_trim($_POST['headline']),ENT_QUOTES)."', byline = '".htmlspecialchars (xoops_trim($_POST['byline']),ENT_QUOTES)."', dateline = '".htmlspecialchars (xoops_trim($_POST['dateline']),ENT_QUOTES)."', tease = '".htmlspecialchars (xoops_trim($_POST['tease']),ENT_QUOTES)."', ".isSoundfile('true', '', 1)." syndicate = '".$_POST['syndicate']."', copyright = '".xoops_trim($_POST['copyright'])."', publish_date = '".$publish_date."', expiration_date = '".xoops_trim($expiration_date)."', ".isImagefile('true','',1)." imageTNwidth = '$imageTNwidth', credit = '".htmlspecialchars (xoops_trim($_POST['credit']),ENT_QUOTES)."', caption = '".htmlspecialchars (xoops_trim($_POST['caption']),ENT_QUOTES)."', body = '".htmlspecialchars (xoops_trim($_POST['body']),ENT_QUOTES)."' WHERE ID = '".$_POST['ID']."'";
			 mysql_select_db($xoopsModuleConfig['syndicationDB']) or ($error = "Could not select database");
			 $result = mysql_query ($Query, $link) or ($error = "Query failed");
			if ($result){
				$syndicateMessage = 'This story was succesfully updated in the syndication database.';
			}else {
				$syndicateMessage = 'There was a problem updating this story in the syndication Database.  Check your syndication DB settings and try again:<P><B><FONT COLOR=\'red\'>'.$error.'</FONT></B>';
			}
		}else{//Leave alone - no syndication at this time
			$storyID = "";
		}
			mysql_free_result($result);
			mysql_close ($link);
		//##############################################
		//############### END DEAL WITH SYNDICATION DB #
		//##############################################	
			
			
		//##############################################
		//############### DEAL WITH LOCAL DB #####
		//##############################################			
			if(!isset($_POST['regionalOnly'])){
				$xoopsDB->query("UPDATE " . $xoopsDB->prefix('local_news_stories') . " SET storyID = '$storyID', headline = '".htmlspecialchars (xoops_trim($_POST['headline']),ENT_QUOTES)."', byline = '".htmlspecialchars (xoops_trim($_POST['byline']),ENT_QUOTES)."', dateline = '".htmlspecialchars (xoops_trim($_POST['dateline']),ENT_QUOTES)."', tease = '".htmlspecialchars (xoops_trim($_POST['tease']),ENT_QUOTES)."', ".isSoundfile('false','old')." syndicate = '".$_POST['syndicate']."', copyright = '".xoops_trim($_POST['copyright'])."', publish_date = '".$publish_date."', expiration_date = '".xoops_trim($expiration_date)."', ".isImagefile('false','old')." imageTNwidth = '$imageTNwidth', credit = '".htmlspecialchars (xoops_trim($_POST['credit']),ENT_QUOTES)."', caption = '".htmlspecialchars (xoops_trim($_POST['caption']),ENT_QUOTES)."', body = '".htmlspecialchars (xoops_trim($_POST['body']),ENT_QUOTES)."' WHERE ID = '".$_POST['ID']."'");
			}
		//##############################################
		//############### END DEAL WITH LOCAL DB #####
		//##############################################					
			if(!isset($_POST['regionalOnly'])){//go back to manage local stories
				redirect_header('index.php?op=manageLocal&currentID=1', 1, "You have succesfully updated the following new story: ".xoops_trim($_POST['headline'])."<P>$syndicateMessage");
			}else{//go back to manage regional stories
				redirect_header('index.php?op=manageRegional&currentID=3', 1, "You have succesfully updated the following new story: ".xoops_trim($_POST['headline'])."<P>$syndicateMessage");
			}
		}else{//insert a new story##########################################
			
			
			//INSETR INTO SYNDICATION DB
			if($_POST['syndicate'] == 1){//never been syndicated before - Create unique Story ID for syndication
						if($fileName != ''){
						$syndicationFileName = XOOPS_URL_NON_SSL."/modules/local_news/media/audio/".$fileName;
					  }else{
						$syndicationFileName = "";
					  }
					  if(isset($image) && $image != ''){
						$syndicationImage = XOOPS_URL_NON_SSL."/modules/local_news/media/pictures/".$image;
					  }else{
						$syndicationImage = "";
					  }
					  
					if(!isset($_POST['regionalOnly'])){//use station Letters to generate storyID 
					  $storyID = $xoopsModuleConfig['stationLetters'] . "-".date("dis");
					 }else{
					  $storyID = "REGI" . "-".date("dis");
					 }					
					   
					$Query = "INSERT INTO CA_SE_News values ('0','$storyID','".htmlspecialchars (xoops_trim($_POST['headline']),ENT_QUOTES)."','".htmlspecialchars (xoops_trim($_POST['byline']),ENT_QUOTES)."','".htmlspecialchars (xoops_trim($_POST['dateline']),ENT_QUOTES)."','".htmlspecialchars (xoops_trim($_POST['tease']),ENT_QUOTES)."','$syndicationFileName','".$_POST['syndicate']."','".xoops_trim($_POST['copyright'])."','".$publish_date."','".xoops_trim($expiration_date)."','$syndicationImage','$imageTNwidth','".xoops_trim($_POST['credit'])."','".htmlspecialchars (xoops_trim($_POST['caption']),ENT_QUOTES)."','".htmlspecialchars (xoops_trim($_POST['body']),ENT_QUOTES)."')";
					mysql_select_db($xoopsModuleConfig['syndicationDB']) or ($error = "Could not select database");
					$result = mysql_query ($Query, $link) or ($error = "Query failed");
					if ($result){
						$syndicateMessage = 'This story was succesfully syndicated.';
					}else {
						$syndicateMessage = 'There was a problem syndicating this story.  Check your syndication DB settings and try again. '.$error.'</FONT></B>';
					}
		mysql_close ($link);
		 }//INSERT INTO LOCAL DB
   			if(!isset($_POST['regionalOnly'])){//only put in local DB if not posting only to the regional DB
				$xoopsDB->query("INSERT INTO " . $xoopsDB->prefix('local_news_stories') . " values ('0','$storyID','".htmlspecialchars (xoops_trim($_POST['headline']),ENT_QUOTES)."','".htmlspecialchars (xoops_trim($_POST['byline']),ENT_QUOTES)."','".htmlspecialchars (xoops_trim($_POST['dateline']),ENT_QUOTES)."','".htmlspecialchars (xoops_trim($_POST['tease']),ENT_QUOTES)."','".xoops_trim($fileName)."','".$_POST['syndicate']."','".xoops_trim($_POST['copyright'])."','".$publish_date."','".xoops_trim($expiration_date)."','".xoops_trim($image)."','$imageTNwidth','".htmlspecialchars (xoops_trim($_POST['credit']),ENT_QUOTES)."','".htmlspecialchars (xoops_trim($_POST['caption']),ENT_QUOTES)."','".htmlspecialchars (xoops_trim($_POST['body']),ENT_QUOTES)."')"); 
				redirect_header('index.php?op=post&currentID=0', 1, "You have succesfully added the following new story: ".xoops_trim($_POST['headline'])."<P>$syndicateMessage");
			}else{
				redirect_header('index.php?op=post&currentID=2', 1, "You have succesfully added the following new story: ".xoops_trim($_POST['headline'])."<P>$syndicateMessage");
			}
		}

}elseif($action=='deleteStory'){
//##########################################################################################
//##########################################################################################
//##########################################################################################
//##########################################################################################
//Delete associated Files
if($_POST['regionalOnly'] == ""){
//#########################################
////delete local stuff
//#########################################
        $result = $xoopsDB->query( "SELECT image, soundfile, storyID FROM ".$xoopsDB->prefix("local_news_stories")." WHERE ID = '".$_POST['ID']."'" ) ;
		if ( $result ) {
			$myrow = $xoopsDB->fetchArray($result);
		}	
		unlink(XOOPS_ROOT_PATH."/modules/local_news/media/audio/".$myrow['soundfile']);
		unlink(XOOPS_ROOT_PATH."/modules/local_news/media/pictures/".$myrow['image']);
		unlink(XOOPS_ROOT_PATH."/modules/local_news/media/pictures/".substr_replace($myrow['image'], '-TN.jpg', -4));
	$xoopsDB->query("DELETE FROM " . $xoopsDB->prefix('local_news_stories') . " WHERE ID='".$_POST['ID']."'");

	//DELETE FROM SYNDICATION DB IF SYNDICATED - non regional only post
	if($myrow['storyID'] != ''){
			$Query = "DELETE FROM CA_SE_News WHERE storyID = '".$myrow['storyID']."'";
			mysql_select_db($xoopsModuleConfig['syndicationDB']) or ($error = "Could not select database");
			$result = mysql_query ($Query, $link) or ($error = "Query failed");
			if ($result){
				$syndicateMessage = 'This story was succesfully removed from syndication.';
			}else {
				$syndicateMessage = 'There was a problem removing this story from syndication.  Check your syndication DB settings and try again.';
			}
	}
	
	if($_POST['title']){
   		redirect_header('index.php?op=manageLocal&currentID=1', 1, "You have succesfully deleted this story: ".stripslashes($_POST['title'])."<P>$syndicateMessage");
	}else{
    	redirect_header('index.php?op=manageLocal&currentID=1', 1, "You have succesfully deleted the selected story.<P>$syndicateMessage");
	}
//#########################################
////END DELETE LOCAL STUFF
//#########################################	
}elseif(isset($_POST['regionalOnly'])){//delete regional only stuff
	$Query = "SELECT image, soundfile, storyID FROM CA_SE_News WHERE ID = '".$_POST['ID']."'";
	mysql_select_db($xoopsModuleConfig['syndicationDB']) or ($error = "Could not select database");
	$result = mysql_query ($Query, $link) or ($error = "Query failed");
	$myrow = mysql_fetch_array($result, MYSQL_ASSOC);
	
	//delete soundfile
	if(eregi('kcaw', $myrow['soundfile'])){
		unlink('/home/coastalaska/kcaw/modules/local_news/media/audio/'.str_replace ("/", "", strrchr ($myrow['soundfile'], "/")));
	}elseif(eregi('kfsk', $myrow['soundfile'])){
		unlink('/home/coastalaska/kfsk/modules/local_news/media/audio/'.str_replace ("/", "", strrchr ($myrow['soundfile'], "/")));
	}elseif(eregi('krbd', $myrow['soundfile'])){
		unlink('/home/coastalaska/krbd/modules/local_news/media/audio/'.str_replace ("/", "", strrchr ($myrow['soundfile'], "/")));
	}elseif(eregi('kstk', $myrow['soundfile'])){
		unlink('/home/coastalaska/kstk/modules/local_news/media/audio/'.str_replace ("/", "", strrchr ($myrow['soundfile'], "/")));
	}
	
	//delete image file
	if(eregi('kcaw', $myrow['image'])){
		unlink('/home/coastalaska/kcaw/modules/local_news/media/pictures/'.str_replace ("/", "", strrchr ($myrow['image'], "/")));
		unlink('/home/coastalaska/kcaw/modules/local_news/media/pictures/'.substr_replace(str_replace ("/", "", strrchr ($myrow['image'], "/")), '-TN.jpg', -4));
	}elseif(eregi('kfsk', $myrow['image'])){
		unlink('/home/coastalaska/kfsk/modules/local_news/media/pictures/'.str_replace ("/", "", strrchr ($myrow['image'], "/")));
		unlink('/home/coastalaska/kfsk/modules/local_news/media/pictures/'.substr_replace(str_replace ("/", "", strrchr ($myrow['image'], "/")), '-TN.jpg', -4));
	}elseif(eregi('krbd', $myrow['image'])){
		unlink('/home/coastalaska/krbd/modules/local_news/media/pictures/'.str_replace ("/", "", strrchr ($myrow['image'], "/")));
		unlink('/home/coastalaska/krbd/modules/local_news/media/pictures/'.substr_replace(str_replace ("/", "", strrchr ($myrow['image'], "/")), '-TN.jpg', -4));
	}elseif(eregi('kstk', $myrow['image'])){
		unlink('/home/coastalaska/kstk/modules/local_news/media/pictures/'.str_replace ("/", "", strrchr ($myrow['image'], "/")));
		unlink('/home/coastalaska/kstk/modules/local_news/media/pictures/'.substr_replace(str_replace ("/", "", strrchr ($myrow['image'], "/")), '-TN.jpg', -4));
	}
	
	//finally delete DB Entry
	$Query = "DELETE FROM CA_SE_News WHERE ID = '".$_POST['ID']."'";
	mysql_select_db($xoopsModuleConfig['syndicationDB']) or ($error = "Could not select database");
	$result = mysql_query ($Query, $link) or ($error = "Query failed");
	if($_POST['title']){
   		redirect_header('index.php?op=manageRegional&currentID=3', 1, "You have succesfully deleted this story: ".stripslashes($_POST['title']));
	}else{
    	redirect_header('index.php?op=manageRegional&currentID=3', 1, "You have succesfully deleted the selected story.");
	}
	
}




}elseif($_GET['action']=='previewStory'){//

//##########################################################################################
//##########################################################################################
//#################################   AJAX PREVIEW IN ADMIN   ##############################
//########################################################################################## 

	function getCopyright($date,$copyright){
	  	if($copyright){
	  		return "&copy; Copyright ".date("Y",$date).", ".$copyright;
	  	}else{
	  		return "&nbsp;";
	  	}
	  		  	
	 }


if($_GET['regionalOnly']!='true'){
	
 $result = $xoopsDB->query( "SELECT * FROM ".$xoopsDB->prefix("local_news_stories")." WHERE ID = '".$_GET['ID']."'" ) ;
 	if ($result) {$myrow = $xoopsDB->fetchArray($result);}//end if ($result)
			header('Content-type: text/html; charset=ISO-8859-1');
			if($myrow['image'] == ""){//NO IMAGE
			echo "<DIV CLASS='headline-large' id='headlinePreview'>".$myrow['headline']."</DIV>
				 <div id='bylinePreview'>".$myrow['byline']."</div>";
				 if($myrow['soundfile']){
					echo"<div id='soundClip' style='margin-top:2px;'><a href='javascript:void(0);' onClick=\"displayMediaPlayer('".$myrow['soundfile']."')\"><img src='../images/listen2.gif'></a></div>";
				}
				echo" <div id='container' style='margin-top:10px;margin-bottom:10px;'>".$myrow['dateline']." (".date("Y-m-d",$myrow['publish_date']).") ". $myts->displayTarea($myrow['tease']);
					if($myrow['body']){
						echo " <a href='javascript:void(0);' onClick=\"addBody(); \" id='moreLink'>more...</a><div id='bodyHolder' style='display:none;'><br />".trim(html_entity_decode($myrow['body']))."</div>";
					}
				echo"<div id='copy' style='padding-top:5px;' CLASS='article-small' >" .getCopyright($myrow['publish_date'],$myrow['copyright']). "</div>
				</div>"; 
			}else{					
			
			echo "<DIV CLASS='headline-large' id='headlinePreview'>".$myrow['headline']."</DIV>
				 <div id='bylinePreview'>".$myrow['byline']."</div>";
				 if($myrow['soundfile']){
					echo"<div id='soundClip' style='margin-top:2px;'><a href='javascript:void(0);' onClick=\"displayMediaPlayer('".$myrow['soundfile']."')\"><img src='../images/listen2.gif'></a></div>";
				}
				echo" <div id='container' style='margin-top:10px;margin-bottom:10px;'>
					 <div style='width:".$myrow['imageTNwidth']."px;float:left;padding-right:5px;padding-bottom:5px;' > 
						<div id='image'><a href='../media/pictures/".$myrow['image']."' TARGET='_blank'> <img src='../media/pictures/".substr_replace($myrow['image'], '-TN.jpg', -4)."' style='float:left; margin-right:3px; margin-bottom:0px; border:1px solid #000000;'></a></div>";
						if($myrow['credit']){
							echo "<div id='credit' CLASS='photo-credit' style='width:".$myrow['imageTNwidth']."px;'>Photo By:".$myrow['credit']."</div>";
						}			
						echo"<div id='caption' CLASS='photo-caption' style='width:".$myrow['imageTNwidth']."px;'>".$myrow['caption']."</div>
					</div>
					".$myrow['dateline']." (".date("Y-m-d",$myrow['publish_date']).") ". $myts->displayTarea($myrow['tease']);
					if($myrow['body']){
						echo " <a href='javascript:void(0);' onClick=\"addBody(); \" id='moreLink'>more...</a><div id='bodyHolder' style='display:none;'><br />".trim(html_entity_decode($myrow['body']))."</div>";
					}
				echo"<div id='copy' style='padding-top:5px;' CLASS='article-small' >" .getCopyright($myrow['publish_date'],$myrow['copyright']). "</div>				</div>";
			}
					
}else{//GET REGIONAL ONLY SHITE
	$Query = "SELECT * FROM CA_SE_News WHERE ID = '".$_GET['ID']."'";
	mysql_select_db($xoopsModuleConfig['syndicationDB']) or ($error = "Could not select database");
	$result = mysql_query ($Query, $link) or ($error = "Query failed");
	$myrow = mysql_fetch_array($result, MYSQL_ASSOC);
            header('Content-type: text/html; charset=ISO-8859-1');
			if($myrow['image'] == ""){//NO IMAGE
			echo "<DIV CLASS='headline-large' id='headlinePreview'>".$myrow['headline']."</DIV>
				 <div id='bylinePreview'>".$myrow['byline']."</div>";
				 if($myrow['soundfile']){
					echo"<div id='soundClip' style='margin-top:2px;'><a href='javascript:void(0);' onClick=\"displayMediaPlayer('".$myrow['soundfile']."','true')\"><img src='../images/listen2.gif'></a></div>";
				}
				echo" <div id='container' style='margin-top:10px;margin-bottom:10px;'>".$myrow['dateline']." (".date("Y-m-d",$myrow['publish_date']).") ". $myts->displayTarea($myrow['tease']);
					if($myrow['body']){
						echo " <a href='javascript:void(0);' onClick=\"addBody(); \" id='moreLink'>more...</a><div id='bodyHolder' style='display:none;'><br />".trim(html_entity_decode($myrow['body']))."</div>";
					}
				echo"<div id='copy' style='padding-top:5px;' CLASS='article-small' >" .getCopyright($myrow['publish_date'],$myrow['copyright']). "</div>
				</div>";
			}else{	
			echo "<DIV CLASS='headline-large' id='headlinePreview'>".$myrow['headline']."</DIV>
				 <div id='bylinePreview'>".$myrow['byline']."</div>";
				 if($myrow['soundfile']){
					echo"<div id='soundClip' style='margin-top:2px;'><a href='javascript:void(0);' onClick=\"displayMediaPlayer('".$myrow['soundfile']."','true')\"><img src='../images/listen2.gif'></a></div>";
				}
				echo" <div id='container' style='margin-top:10px;margin-bottom:10px;'>
					 <div style='width:".$myrow['imageTNwidth']."px;float:left;padding-right:5px;padding-bottom:5px;' > 
						<div id='image'><a href='".$myrow['image']."' TARGET='_blank'> <img src='".substr_replace($myrow['image'], '-TN.jpg', -4)."' style='float:left; margin-right:3px; margin-bottom:0px; border:1px solid #000000;'></a></div>";
						if($myrow['credit']){
							echo "<div id='credit' CLASS='photo-credit' style='width:".$myrow['imageTNwidth']."px;'>Photo By:".$myrow['credit']."</div>";
						}			
						echo"<div id='caption' CLASS='photo-caption' style='width:".$myrow['imageTNwidth']."px;'>".$myrow['caption']."</div>
					</div>
					".$myrow['dateline']." (".date("Y-m-d",$myrow['publish_date']).") ". $myts->displayTarea($myrow['tease']);
					if($myrow['body']){
						echo " <a href='javascript:void(0);' onClick=\"addBody(); \" id='moreLink'>more...</a><div id='bodyHolder' style='display:none;'><br />".trim(html_entity_decode($myrow['body']))."</div>";
					}
				echo"<div id='copy' style='padding-top:5px;' CLASS='article-small' >" .getCopyright($myrow['publish_date'],$myrow['copyright']). "</div>
				</div>";
			}	
	
}


}
?>